package com.atguigu.lease.common.exception;

import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.common.result.ResultCodeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {
    private Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(RuntimeException.class)
    public Result error(Exception exception){
        logger.info(exception.getMessage());
        exception.printStackTrace();
        return Result.fail();
    }

    @ExceptionHandler(LeaseException.class)
    public Result myError(LeaseException exception){
        ResultCodeEnum resultCodeEnum = exception.getResultCodeEnum();
        logger.info(resultCodeEnum.getCode()+":::"+resultCodeEnum.getMessage());
        exception.printStackTrace();
        return Result.build(null, resultCodeEnum);
    }
}
